package com.suitsoft.ut.web;



import java.io.File;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;
import com.suitsoft.ut.util.upload.UploadFileUtil;
import com.suitsoft.ut.util.upload.UploaderDBUtil;
import com.suitsoft.ut.util.upload.XdbFiles;


/**
 * 测试上传
 *@author Suineg
 */
@SuppressWarnings("serial")
public class TestUploadAction extends ActionSupport{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private Long id;
	
	/** 上传文件 */
	private File uploadFile;
	/** 上传文件名 */
	private String uploadFileFileName;
	
	/**
	 * 上传
	 * @return
	 * @throws Exception
	 */
	public String upload() throws Exception {
		if(uploadFile == null){
			Struts2Utils.getResponse().sendError(502, "有出错啦");
		}else{
			logger.info("id:" + id);
			ServletContext sc = ServletActionContext.getServletContext();
			UploadFileUtil fileUtil = new UploadFileUtil(sc,uploadFileFileName);
			fileUtil.CreateTmpFileName();
			File tempFileName = new File(fileUtil.getNewFileNamePath());
			uploadFile.renameTo(tempFileName);
			XdbFiles xdbFile = new XdbFiles(tempFileName,uploadFileFileName);
			xdbFile.setUid(Integer.valueOf(id.toString()));
			UploaderDBUtil dbUtil = new UploaderDBUtil(ServletActionContext.getServletContext());
			dbUtil.add(xdbFile);
		}
		return SUCCESS;
	}
	/**
	 * 测试获取文件信息
	 * @return
	 */
	public String testGetFileInfo(){
		UploaderDBUtil dbUtil = new UploaderDBUtil(ServletActionContext.getServletContext());
		XdbFiles files = dbUtil.getFileInfByFid(1);
		logger.info(files.toString());
		Struts2Utils.getRequest().setAttribute("file", files.toString());
		return SUCCESS;
	}
	public File getUploadFile() {
		return uploadFile;
	}
	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}
	public String getUploadFileFileName() {
		return uploadFileFileName;
	}
	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
}


